package com.sq.partner.manager.controller;

import com.sq.partner.manager.common.Constants;
import com.sq.partner.manager.common.Result;
import com.sq.partner.manager.mybatis.model.GatewayUser;
import com.sq.partner.manager.service.CtripService;
import com.sq.partner.manager.util.LogUtil;
import com.sq.partner.manager.util.Page;
import com.sq.partner.manager.util.SqlUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/ctrip")
public class CtripController {

    private static Logger logger = LogUtil.getLog(CtripController.class);

    @Autowired
    private CtripService ctripService;

    @RequestMapping("/index")
    public String index(){
        return "/ctrip/index";
    }

    @RequestMapping("/queryStatisticsPage")
    @ResponseBody
    public Page queryStatisticsPage(HttpServletRequest request,Page page,String dateBegin,String dateEnd) throws Exception{
        HttpSession session = request.getSession();
        GatewayUser user = (GatewayUser)session.getAttribute(Constants.SESSION_USER);
        String deptStr = user.getDepartmentStr();
        List<String>  deptList = Arrays.asList(deptStr.split(","));
        for (String s : deptList) {
            if(s.startsWith("'00")){
                deptStr = "";
                break;
            }
        }
        Map<String, Object> paramMap = SqlUtil.buildPageMap(page);
        paramMap.put("dateBegin", dateBegin);
        paramMap.put("dateEnd", dateEnd);
        paramMap.put("deptStr", deptStr);
        page.setMap(paramMap);
        page = ctripService.queryStatisticsPage(page);
        return page;
    }

    /**
     * 跳转详情页面
     * @param map
     * @return
     */
    @RequestMapping("/detail")
    public String toDetail(HttpServletRequest request, ModelMap map) throws Exception {
        map.put("effecttime", request.getParameter("effecttime"));
        map.put("failPremium", request.getParameter("failPremium"));
        map.put("count", request.getParameter("count"));
        return "ctrip/detail";
    }

    @RequestMapping("/queryPolicyPageByEffecttime")
    @ResponseBody
    public Page queryPolicyPageByEffecttime(HttpServletRequest request,Page page,String effecttime) throws Exception {
        HttpSession session = request.getSession();
        GatewayUser user = (GatewayUser)session.getAttribute(Constants.SESSION_USER);
        String deptStr = user.getDepartmentStr();
        List<String>  deptList = Arrays.asList(deptStr.split(","));
        for (String s : deptList) {
            if(s.startsWith("'00")){
                deptStr = "";
                break;
            }
        }
        Map<String, Object> paramMap = SqlUtil.buildPageMap(page);
        paramMap.put("effecttime", effecttime);
        paramMap.put("deptStr", deptStr);
        page.setMap(paramMap);
        page = ctripService.queryPolicyPageByEffecttime(page);
        return page;
    }

    @RequestMapping("/downloadExcel/{effecttime}")
    public void downloadExcel(HttpServletRequest request,HttpServletResponse response, Page page,@PathVariable String effecttime) throws Exception {
        HttpSession session = request.getSession();
        GatewayUser user = (GatewayUser)session.getAttribute(Constants.SESSION_USER);
        String deptStr = user.getDepartmentStr();
        List<String>  deptList = Arrays.asList(deptStr.split(","));
        for (String s : deptList) {
            if(s.startsWith("'00")){
                deptStr = "";
                break;
            }
        }
        Map<String, Object> paramMap = SqlUtil.buildPageMap(page);
        paramMap.put("effecttime", effecttime);
        paramMap.put("deptStr", deptStr);
        page.setMap(paramMap);
        List<Map<String, Object>> ctripList = ctripService.queryAllPolicyByEffecttime(page);
        ctripService.downloadExcel(ctripList,response);
    }

}
